package com.tools.middle.elasticsearch;

import com.tools.common.object.Note;

import java.util.Locale;

/**
 * ElasticSearch 批量操作文档时
 * 决定该次请求的操作方式的枚举
 * */
@Note("ElasticSearch 批量操作文档时，决定该次请求的操作方式的枚举")
public enum ESDocBulkOpEnum {

    CREATE(0, "create", "文档不存在就创建，存在就报错"),
    INDEX(1, "index", "文档不存在就创建，存在就更新"),
    UPDATE(2, "update", "更新文档，文档不存在就报错"),
    DELETE(3, "delete", "删除文档，文档不存在就报错");

    private final int code;

    private final String value;

    ESDocBulkOpEnum(int code, String value, String explain) {
        this.code = code;
        this.value = value;
        explain = null;
    }

    public int getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }

    public boolean eqValue(String value) {
        return this.value.equals(value);
    }

    public boolean eqIgnoreCase(String value) {
        return this.value.equalsIgnoreCase(value);
    }

    @Override
    public String toString() {
        return value;
    }

    @Note("安全地根据代号获取对应的枚举实例，获取不到不会抛异常")
    public static ESDocBulkOpEnum getEnumSafe(int code) {
        switch (code) {
            case 0 : return CREATE;
            case 1 : return INDEX;
            case 2 : return UPDATE;
            case 3 : return DELETE;
        }
        return null;
    }

    @Note("根据代号获取对应的枚举实例，获取不到则抛异常")
    public static ESDocBulkOpEnum getEnum(int code) {
        ESDocBulkOpEnum bulkOp = getEnumSafe(code);
        if(bulkOp == null) throw new IllegalArgumentException("不知道 " + code + " 对应什么枚举实例");
        return bulkOp;
    }


    @Note("安全地根据名称获取对应的枚举实例，获取不到不会抛异常")
    public static ESDocBulkOpEnum getEnumSafe(String value) {
        switch (value.toLowerCase(Locale.ROOT)) {
            case "create": return CREATE;
            case "index": return INDEX;
            case "update": return UPDATE;
            case "delete": return DELETE;
            default: return null;
        }
    }

    @Note("根据名称获取对应的枚举实例，获取不到则抛异常")
    public static ESDocBulkOpEnum getEnum(String value) {
        ESDocBulkOpEnum bulkOp = getEnumSafe(value);
        if(bulkOp == null) throw new IllegalArgumentException("不知道 " + value + " 对应什么枚举实例");
        return bulkOp;
    }
}
